package com.qf.student.util;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @version1.0
 * @auth dmh
 */
public class TxManager {
      //1.开启事务

        //2.操作数据1
        //.操作数据2
        //3.提交事务/回滚
    public static void begin(){
        Connection connection=DBMangaerUtils.getConnection();
        try {
            connection.setAutoCommit(false);
            ConnectionThreadLocal.set(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public static void commit(){
        try {
            Connection connection = ConnectionThreadLocal.get();
            if (connection != null) {
                connection.commit();
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        }

    public static void rollback(){
        try {
            Connection connection = ConnectionThreadLocal.get();
            if (connection != null) {
                connection.rollback();
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
